package com.masterclient.app.widget;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.eoner.baselibrary.utils.FastBlur;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

/**
 * 作者：sean
 * 创建时间：2017/7/4 15:51
 * 功能说明：
 */

public class BlurTarget implements Target{
    ImageView imageView;
    public BlurTarget(ImageView imageView) {
        this.imageView = imageView;
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        imageView.setImageBitmap(blur(bitmap));
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {

    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }

    private Bitmap blur(Bitmap bkg) {
        long startMs = System.currentTimeMillis();
        float scaleFactor = 8;
        float radius = 2;

        Bitmap overlay = Bitmap.createBitmap((int) (750/scaleFactor),
                (int) (268/scaleFactor), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(overlay);
        canvas.translate(0,-20);
        canvas.scale(0.8f, 0.8f);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(bkg, 0, 0, paint);

        overlay = FastBlur.doBlur(overlay, (int)radius, true);
        return overlay;

    }
}
